<template>
  <el-card>
    <div slot="header"><span>订单管理</span></div>
    <!-- 查询表单 -->
    <el-form inline :model="formData" size="mini">
      <el-form-item label="订单号">
        <el-input v-model="formData.orderNo" placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="收货人">
        <el-input v-model="formData.consignee" placeholder="收货人"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formData.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="formData.orderState">
          <el-option value="已受理" label="已受理"></el-option>
          <el-option value="派送中" label="派送中"></el-option>
          <el-option value="已完成" label="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择时间">
        <!-- value-format 值格式 -->
        <!-- format 显示格式 -->
        <el-date-picker
          v-model="formData.date"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="search" type="primary">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表表格 -->

    <!-- 
    id	订单id
    orderNo	订单号
    orderTime	下单时间
    phone	联系电话
    consignee	收货人
    deliverAddress	送货地址
    deliveryTime	送达时间
    remarks	备注
    orderAmount	订单金额
    orderState	订单状态
     -->
    <el-table :data="tableData" border :style="{width: w + 'px'}">
      <el-table-column label="订单号" prop="orderNo"></el-table-column>
      <el-table-column label="下单时间">
        <template slot-scope="scope">
          {{ scope.row.orderTime | formatHms }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
      <el-table-column label="收货人" prop="consignee"></el-table-column>
      <el-table-column label="送货地址" prop="deliverAddress"></el-table-column>
      <el-table-column label="送达时间">
        <template slot-scope="scope">
          {{ scope.row.deliveryTime | formatHms }}
        </template>
      </el-table-column>
      <el-table-column label="用户备注" prop="remarks"></el-table-column>
      <el-table-column label="订单金额" prop="orderAmount"></el-table-column>
      <el-table-column label="订单状态" prop="orderState"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="
              $router.push({
                path: '/order/order-detail',
                query: {id: scope.row.id},
              })
            "
            size="small"
          >
            查看
          </el-button>
          <el-button
            type="text"
            @click="$router.push(`/order/order-edit/${scope.row.id}`)"
            size="small"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="mt20"
      :page-size="pageSize"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
      background
      layout="total,prev,pager,next,jumper"
    ></el-pagination>
  </el-card>
</template>

<script>
import {getOrderListReq} from '@/api/order'

//引入过滤器函数
import {formatYMDhms} from '@/filters'

//引入mixins
import resizeTable from '@/mixins/resizeTable'
export default {
  mixins: [resizeTable],
  data() {
    return {
      //查询表单数据
      formData: {
        orderNo: '', //订单号
        consignee: '', //收货人
        phone: '', //手机号
        orderState: '', //订单状态
        date: [], //日期
      },
      //查询表单数据
      searchForm: {},

      //表格数据
      tableData: [],
      //分页数据
      currentPage: 1,
      pageSize: 3,
      total: 0,
      w: 0, //table宽度
    }
  },
  methods: {
    async getData() {
      let currentPage = this.currentPage
      let pageSize = this.pageSize
      //处理日期格式 处理默认值null问题
      let date = JSON.stringify(this.searchForm.date || '')
      //发送请求
      let res = await getOrderListReq({
        currentPage,
        pageSize,
        ...this.searchForm, //查询表单数据进来
        date,
      })
      //解构数据
      let {total, data} = res.data
      //数据赋值
      this.total = total
      this.tableData = data
    },
    //查询
    search() {
      //把当前页重置为1
      this.currentPage = 1

      //把当前formData的值 拷贝给查询表单使用
      this.searchForm = {...this.formData}

      this.getData()
    },
    //改变当前页码
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    },
  },
  created() {
    this.getData()
  },
}
</script>

<style lang="scss" scoped></style>
